# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005-2006 CamptoCamp
# Copyright (c) 2006-2010 OpenERP S.A
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import os, time
from openerp.osv import osv, fields
from openerp.tools.translate import _
from openerp.report import report_sxw
import locale
locale.setlocale(locale.LC_ALL, '')

class daily_timesheet_report(report_sxw.rml_parse):
    _name = 'daily.timesheet.report'

    def set_context(self, objects, data, ids, project_id=None):
        new_ids = ids
        project_task_obj = self.pool.get('project.task')
        emp_obj = self.pool.get('hr.employee')
        
        project_name1 = project_name2 = ''
        task_name1 = task_name2 = ''
        task_detail1 = task_detail2 = ''
        emp_name = emp_no = emp_dept = ''
        cur_date = False
        
        project_task_ids = data['form']['project_task_ids']
        count = 0    
        if project_task_ids:
            for pt in project_task_obj.browse(self.cr, self.uid, project_task_ids):
                count +=1
                if count == 1:
                    project_name1 = pt.project_id.name or ''
                    task_name1 = pt.name or ''
                    task_detail1 = pt.description or ''
                if count == 2:
                    project_name2 = pt.project_id.name or ''
                    task_name2 = pt.name or ''
                    task_detail2 = pt.description or ''
        
        cur_date = time.strftime("%d/%m/%Y")
        emp_id = emp_obj.search(self.cr, self.uid, [('user_id', '=', self.uid)])

        emp_name = emp_obj.browse(self.cr, self.uid, emp_id).name or ''
        emp_no = emp_obj.browse(self.cr, self.uid, emp_id).employee_no or ''
        emp_dept = emp_obj.browse(self.cr, self.uid, emp_id).department_id.name or ''
        
        if not emp_id:
            user_name = self.pool.get('res.users').read(self.cr, self.uid, [emp_id], ['name'])[0]['name']
            raise osv.except_osv(_('Bad Configuration!'),
                 _('Please define employee for user "%s". You must create one.')% (user_name,))
        
        self.count = count 
        self.project_name1 = project_name1
        self.project_name2 = project_name2
        self.task_name1 = task_name1
        self.task_name2 = task_name2
        self.task_detail1 = task_detail1
        self.task_detail2 = task_detail2
        self.emp_name = emp_name
        self.emp_no = emp_no
        self.emp_dept = emp_dept
        self.cur_date = cur_date
        
        return super(daily_timesheet_report, self).set_context(objects, data, new_ids)

    def __init__(self, cr, uid, name, context=None):
        super(daily_timesheet_report, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'locale': locale,
            'get_project_name': self._get_project_name,
            'get_project_name2': self._get_project_name2,
            'get_task_name1': self._get_task_name1, 
            'get_task_name2': self._get_task_name2, 
            'get_task_detail1': self._get_task_detail1, 
            'get_task_detail2': self._get_task_detail2, 
            'get_emp_name': self._get_emp_name,
            'get_emp_name2': self._get_emp_name2,
            'get_emp_no': self._get_emp_no, 
            'get_emp_no2': self._get_emp_no2,
            'get_emp_dept': self._get_emp_dept,
            'get_emp_dept2': self._get_emp_dept2,
            'get_cur_date': self._get_cur_date, 
            'get_cur_date2': self._get_cur_date2, 
            })
    
    def _get_project_name(self):
        project_name = self.project_name1
        return project_name

    def _get_project_name2(self):
        if self.count > 1:
            project_name = self.project_name2
        else:
            project_name = ''
        return project_name
    
    def _get_task_name1(self):
        task_name1 = self.task_name1
        return task_name1
    
    def _get_task_name2(self):
        task_name2 = self.task_name2
        return task_name2
    
    def _get_task_detail1(self):
        task_detail1 = self.task_detail1
        return task_detail1
    
    def _get_task_detail2(self):
        task_detail2 = self.task_detail2
        return task_detail2
    
    def _get_emp_name(self):
        emp_name = self.emp_name
        return emp_name
    
    def _get_emp_name2(self):
        if self.count > 1:
            emp_name = self.emp_name
        else:
            emp_name = ''
        return emp_name
    
    def _get_emp_no(self):
        emp_no = self.emp_no
        return emp_no
    
    def _get_emp_no2(self):
        if self.count > 1:
            emp_no = self.emp_no
        else:
            emp_no = ''
        return emp_no
    
    def _get_emp_dept(self):
        emp_dept = self.emp_dept
        return emp_dept
    
    def _get_emp_dept2(self):
        if self.count > 1:
            emp_dept = self.emp_dept
        else:
            emp_dept = ''
        return emp_dept
    
    def _get_cur_date(self):
        cur_date = self.cur_date
        return cur_date
    
    def _get_cur_date2(self):
        if self.count > 1:
            cur_date = self.cur_date
        else:
            cur_date = ''
        return cur_date
    
report_sxw.report_sxw('report.daily.timesheet.report', 'param.daily.time.sheet.report',
'addons/batam_fast_module/project/report/report/daily_timesheet_report.rml', parser=daily_timesheet_report, header=False)
